package cn.stylefeng.guns.modular.bulletin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.base.util.RedisUtil;
import cn.stylefeng.guns.modular.bulletin.entity.Subscribe;
import cn.stylefeng.guns.modular.bulletin.entity.SubscribeRecords;
import cn.stylefeng.guns.modular.bulletin.service.SubscribeRecordsService;
import cn.stylefeng.guns.modular.bulletin.service.SubscribeService;
import cn.stylefeng.guns.modular.meta_data.constant.ConfigMap;
import cn.stylefeng.guns.modular.meta_data.entity.Config;
import cn.stylefeng.guns.modular.meta_data.model.ConfigDto;
import cn.stylefeng.guns.modular.meta_data.service.ConfigService;
import cn.stylefeng.guns.modular.meta_data.wrapper.ConfigWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 短信参数配置
 */
@Controller
@RequestMapping("/subscribe")
public class SubscribeController extends BaseController {

    private static String PREFIX = "/modular/bulletin/subscribe/";



    @Autowired
    private SubscribeService subscribeService;

    @Autowired
    private SubscribeRecordsService subscribeRecordsService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 跳转到管理的首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "subscribe.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/records")
    public String records() {
        return PREFIX + "records.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/subscribe_add")
    public String configAdd() {
        return PREFIX + "subscribe_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/subscribe_edit")
    public String configEdit(Long configId, Model model) {
        Subscribe condition = this.subscribeService.getById(configId);
        model.addAllAttributes(BeanUtil.beanToMap(condition));
        LogObjectHolder.me().set(condition);
        return PREFIX+"subscribe_edit.html";
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{configId}")
    @ResponseBody
    public Object detail(@PathVariable("configId") Long configId) {
        Subscribe config = subscribeService.getById(configId);
        ConfigDto conditionDto = new ConfigDto();
        BeanUtil.copyProperties(config, conditionDto);
        return config;
    }

    /**
     * 查询列表
     */
    @ApiOperation("配置列表")
    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String condition) {

        //根据条件查询
        Page<Map<String, Object>> result = subscribeService.selectByCondition(condition);
        Page wrapped = new ConfigWrapper(result).wrap();
        return LayuiPageFactory.createPageInfo(wrapped);
    }


    /**
     * 币种上线
     */
    @ApiOperation("完成申购")
    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping("/onLine/{id}")
    @ResponseBody
    public ResponseData onLine(@PathVariable("id") Long id) {
        //根据条件查询
      Boolean ok =  subscribeService.online(id);
      if(!ok){
          return ResponseData.error("完成申购失败");
      }
        return ResponseData.success();
    }

    /**
     * 编辑
     */
    @RequestMapping("/edit")
    @BussinessLog(value = "编辑参数", key = "id", dict = ConfigMap.class)
    @ResponseBody
    public ResponseData edit(Subscribe subscribe) {

        Subscribe entity=subscribeService.getById(subscribe.getId());
        if (entity == null) {
            return ResponseData.error("参数不能为空");
        }
        subscribeService.updateById(subscribe);
        return SUCCESS_TIP;
    }


    /**
     * 添加参数
     */
    @RequestMapping("/add")
    @BussinessLog(value = "添加参数", key = "name", dict = ConfigMap.class)
    @ResponseBody
    public ResponseData add(Subscribe subscribe, BindingResult result) {

        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        if (subscribe == null) {
            return ResponseData.error("参数不能为空");
        }
        Subscribe news = new Subscribe();
        news.setStatus("Y");
        news= subscribeService.getOne(new QueryWrapper<>(news));
        if(news!=null){
            //return ResponseData.error("只能添加一个IEO币");
        }
        //公共参数
        this.subscribeService.save(subscribe);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @BussinessLog(value = "删除参数", key = "id", dict = ConfigMap.class)
    @ResponseBody
    public ResponseData delete(@RequestParam Long configId) {
        if (ToolUtil.isEmpty(configId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.subscribeService.removeById(configId);
        return SUCCESS_TIP;
    }



    @RequestMapping("/recordslist")
    @ResponseBody
    public Object recordslist( @RequestParam(required = false) String name,@RequestParam(required = false) String account) {
        Page<Map<String, Object>> result = subscribeRecordsService.selectByCondition(name,account);

        Page wrapped = new ConfigWrapper(result).wrap();
        return LayuiPageFactory.createPageInfo(wrapped);


    }


    /**
     * 详情
     */
    @RequestMapping(value = "/recordsDetail/{id}")
    @ResponseBody
    public Object recordsDetail(@PathVariable("id") Long id) {
        SubscribeRecords config = subscribeRecordsService.getById(id);
        ConfigDto conditionDto = new ConfigDto();
        BeanUtil.copyProperties(config, conditionDto);
        return config;
    }


    /**
     * 跳转到修改
     */
    @RequestMapping("/records_edit")
    public String recordsEdit(Long id, Model model) {
//        SubscribeRecords condition = this.subscribeRecordsService.getById(id);
//        model.addAllAttributes(BeanUtil.beanToMap(condition));
//        LogObjectHolder.me().set(condition);
        return PREFIX+"records_edit.html";
    }

    /**
     * 编辑
     */
    @RequestMapping("/recordsEdit")
    @BussinessLog(value = "编辑参数", key = "configId", dict = ConfigMap.class)
    @ResponseBody
    public ResponseData edit(SubscribeRecords subscribeRecords) {

        SubscribeRecords entity=subscribeRecordsService.getById(subscribeRecords.getRecordsId());
        if (entity == null) {
            return ResponseData.error("参数不能为空");
        }
        subscribeRecordsService.updateById(subscribeRecords);
        return SUCCESS_TIP;
    }


}
